// router.js
import {RouterMount,createRouter} from 'uni-simple-router';
import store from "@/store"
import * as utils from '@/utils/utils.js'
const router = createRouter({
	platform: process.env.VUE_APP_PLATFORM,  
	routes: [...ROUTES]
});
//全局路由前置守卫
router.beforeEach((to, from, next) => {
	if (to.meta.auth && !store.state.vuex_token) {
		store.commit('set', {name: 'pre_login_page',value: to.path + uni.$u.queryParams(to.query) });
		if (utils.isWechatBrowser()) {
			let url = '';
			if (window.location.hash != '') {
				url = window.location.origin + window.location.pathname + '?hashpath=/pages/user/wechatauth'
			} else {
				url = window.location.origin + window.location.pathname.replace(/pages.*/,
					'pages/user/login');
			};
			getWechatLoginUrl({
				data: {
					url: url
				}
			}).then(res=>{
				window.location.href = res.data;
			});
			
		}else{
			return next({
				path: '/pages/user/login',
				NAVTYPE: 'push'
			});
		}
	}
	next();
});
// 全局路由后置守卫
router.afterEach((to, from) => {
    console.log('跳转结束')
	
})

export {
	router,
	RouterMount
}